package com.baiytfp.hf.HFDataExchange.action.controller.service;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.baiytfp.hf.HFDataExchange.action.service.PasswordRetrievalService;
import com.baiytfp.hf.HFDataExchange.common.model.table.Password_retrieval;
import com.baiytfp.hf.HFDataExchange.common.model.table.User_info;
import com.baiytfp.hf.HFDataExchange.common.utils.CommonUtil;
import com.baiytfp.hf.HFDataExchange.common.utils.PathInfo;
import com.baiytfp.hf.baiytfp.common.util.DateUtil;
import com.baiytfp.hf.baiytfp.framework.model.state.YesOrNo;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;

/** 
* @ClassName: PasswordRetrievalSController 
* @Description: 找回密码 service controller
* @author 罗赵树
* @date 2017年10月16 下午 04:32:15 by linksoft
*  
*/
@Controller
@RequestMapping("/service/passwordretrieval")
public class PasswordRetrievalSController {
	
	@Autowired
	private PasswordRetrievalService passwordRetrievalService;

	/**
	 * 获取传过来的邮箱, 发送邮件
	 */
	@RequestMapping(value="/sendmail", method= RequestMethod.POST)
	public void sendmail(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String email = HFRequest.getIstance().getString(request, "email");
		//判断邮箱
		User_info user = passwordRetrievalService.getStaffByEmail(email);
		if(user == null) {
			HFResponse.getIstance().returnResult(response, -1, "邮箱不存在", null);
			return;
		}
		//发送邮件
		Password_retrieval bean = HFWeb.getIstance().toBean(request, Password_retrieval.class);
//		String rootPath = request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
		HFResponse.getIstance().returnResultByRes(response,passwordRetrievalService.sendmail(bean, user, PathInfo.getRealmPath(request)));
	}
	
	//-----------
	
	@SuppressWarnings("unused")
	@RequestMapping("/passwordretrieval")
	public void passwordRetrieval(HttpServletRequest request,HttpServletResponse response) throws Exception {
		//有效时间(毫秒), 一小时
		int outtime = 3600000;
		String key = HFRequest.getIstance().getString(request, "key");
		String password = HFRequest.getIstance().getString(request, "password");
		Password_retrieval password_retrieval = passwordRetrievalService.getPasswordRetrievalByKey(key);
		if(password_retrieval == null) {
			HFResponse.getIstance().returnResultByRes(response, -1);
			return;
		} 
		Timestamp create_time = password_retrieval.getCreate_time();
		if(DateUtil.getCurrentDT().getTime() - create_time.getTime() > outtime || password_retrieval.getIs_valid() == YesOrNo.NO.getKey()) {
			HFResponse.getIstance().returnResult(response, -1, "该链接已经失效.", null);
		} else {
			//更新密码
			
			if(!CommonUtil.matches(password, password_retrieval.getUser_code())) {
				HFResponse.getIstance().returnResult(response, -1, "密码须含有至少1个以上的数字、字母、特殊符号，长度大于等于8，且不包含帐号", null);
				return;
			}
			
			HFResponse.getIstance().returnResultByRes(response, 
					passwordRetrievalService.updatePassword(password_retrieval.getId(), password, password_retrieval.getUser_code())
					);
		}
		
		
	}
	
}
